package osx.mantianx.wirte;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * @author starrysky
 * @title: PoiBigTableV03
 * @projectName Excel_Poi_EasyExcel
 * @description: HSSFWorkbook特点：最大行数65536超出会有异常抛出，写入时会先写到内存，再一次性写到文件，速度快
 * @date 2021/2/410:02
 */
public class PoiBigTableV03 {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("数字矩阵表");

        for (int rowNumber = 0; rowNumber < 65539; rowNumber++) {
            Row row = sheet.createRow(rowNumber);
            for (int colNumber = 0; colNumber < 6; colNumber++) {
                Cell cell = row.createCell(colNumber);
                cell.setCellValue(UUID.randomUUID().toString().substring(0,5));
            }
        }
        FileOutputStream xlsBigTablesV03 = null;
        try {
            xlsBigTablesV03 = new FileOutputStream("XlsBigTablesV03_oom.xls");
            workbook.write(xlsBigTablesV03);
            System.out.println("写入完毕");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (xlsBigTablesV03!=null){
                try {
                    xlsBigTablesV03.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("耗时总时间"+(double)(endTime-startTime)/1000);
    }
}
